package org.jsola.hr.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.hr.example.TaskExample.E_DESCRIPTION;
import static org.jsola.hr.example.TaskExample.E_RUN_BEAN_NAME;
import static org.jsola.hr.example.TaskExample.E_RUN_DATE_END;
import static org.jsola.hr.example.TaskExample.E_RUN_DATE_START;
import static org.jsola.hr.example.TaskExample.E_RUN_TYPE;
import static org.jsola.hr.example.TaskExample.E_STATUS;
import static org.jsola.hr.example.TaskExample.E_SUBJECT_ID;
import static org.jsola.hr.example.TaskExample.E_SUBJECT_TYPE;
import static org.jsola.hr.example.TaskExample.N_DESCRIPTION;
import static org.jsola.hr.example.TaskExample.N_RUN_BEAN_NAME;
import static org.jsola.hr.example.TaskExample.N_RUN_DATE_END;
import static org.jsola.hr.example.TaskExample.N_RUN_DATE_START;
import static org.jsola.hr.example.TaskExample.N_RUN_TYPE;
import static org.jsola.hr.example.TaskExample.N_STATUS;
import static org.jsola.hr.example.TaskExample.N_SUBJECT_ID;
import static org.jsola.hr.example.TaskExample.N_SUBJECT_TYPE;

/**
 * 人力定时任务
 *
 * @author zhr
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class TaskQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = "运行参数")
    private String runParam;

    @ApiParam(value = N_SUBJECT_TYPE, example = E_SUBJECT_TYPE)
    @Length(max = 100)
    private String subjectType;

    @ApiParam(value = N_SUBJECT_ID, example = E_SUBJECT_ID)
    private String subjectId;

    @ApiParam(value = N_DESCRIPTION, example = E_DESCRIPTION)
    @Length(max = 100)
    private String description;

    @ApiParam(value = N_RUN_BEAN_NAME, example = E_RUN_BEAN_NAME)
    @Length(max = 200)
    private String runBeanName;

    /**
     * 定时任务运行时间查询范围
     */
    @ApiParam(value = N_RUN_DATE_START, example = E_RUN_DATE_START)
    private Date runDateStart;
    @ApiParam(value = N_RUN_DATE_END, example = E_RUN_DATE_END)
    private Date runDateEnd;


    @ApiParam(value = N_RUN_TYPE, example = E_RUN_TYPE)
    @Length(max = 100)
    private String runType;

    @ApiParam(value = N_STATUS, example = E_STATUS)
    private Boolean status;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public TaskQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

